package com.mogu.search.service.impl;


import com.mogu.entity.TBlog;
import com.mogu.search.mapper.SearchDao;


import com.mogu.search.service.SearchService;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.text.Text;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SerachServiceImpl implements SearchService {


    @Autowired
    private   RestHighLevelClient restClient;
    @Resource
    private SearchDao searchDao;



    @Override
    public List<TBlog> getBlogByTitle(String title) {

        List<TBlog> byTitle = searchDao.getBlogByTitle(title);

        if (title !=null){
            return byTitle;
        }else{
            return null;
        }
    }

    @Override
    public List<TBlog> getBlogByContent(String content) {
        List<TBlog> byContent = searchDao.getBlogByContent(content);
        if (byContent !=null){
            return byContent;
        }else{
            return null;
        }
    }

    @Override
    public List<Map<String, Object>> findAll()throws IOException {

        SearchRequest searchRequest = new SearchRequest("test");
        searchRequest.types("test");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //查询全部
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        //设置boolquery ==>将多个搜索条件组个起来
//        MultiMatchQueryBuilder multiQuery = QueryBuilders.multiMatchQuery(value, "title", "summer", "content");
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//        boolQueryBuilder.must(multiQuery);

//
//        searchSourceBuilder.query(boolQueryBuilder);
        //设置高亮
////        HighlightBuilder highlightBuilder = new HighlightBuilder();
////        highlightBuilder.preTags("<font style='color:red'>");
////        highlightBuilder.postTags("</font>");
////        highlightBuilder.fields().add(new HighlightBuilder.Field("title"));
////        searchSourceBuilder.highlighter(highlightBuilder);
        //设置分页
        int page=1;
        int size=1000;

        int from = (page-1)*size;
        searchSourceBuilder.from(from);
        searchSourceBuilder.size(size);
        //讲搜索源对象设置到搜索客户端，拿到
        searchRequest.source(searchSourceBuilder);
        //使用es客户端进行搜索，拿到搜索的返回值
        SearchResponse search = restClient.search(searchRequest);
        //解析返回值
        SearchHits hits = search.getHits();
        SearchHit[] hits1 = hits.getHits();
        List list = new ArrayList<>();
        for (SearchHit hit:hits1){
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println("当前搜索出的数据ID为======>"+hit.getId()+"=========="+hit.docId());
            //System.out.println(sourceAsMap);
//            Map<String, HighlightField> highlightFields = hit.getHighlightFields();
//            if (highlightFields!=null){
////                HighlightField title = highlightFields.get("title");
////                if (title!=null){
////                    Text[] fragments = title.getFragments();
////                    StringBuffer stringBuffer = new StringBuffer();
////                    for (Text text:fragments){
////                        stringBuffer.append(text);
////                    }
////                    sourceAsMap.put("title",stringBuffer.toString());
////                }
////            }
            list.add(sourceAsMap);
        }
        return list;
    }

}
